//
//  TeacherChildCheckOnCalendar.m
//  iBaby365v2
//
//  Created by Kirinrin on 14/12/20.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "TeacherChildCheckOnCalendar.h"
#import "NetworkAccessManager.h"
#import "NSDate+WQCalendarLogic.h"
#import "WQCalendarTileView.h"
#import "UIAlertView+Kirinrin.h"
#import "AppDelegate.h"


#define kDataKey_childId    @"childId"
#define kDataKey_classId    @"classId"
#define kDataKey_createTime @"createTime"
#define kDataKey_markDate   @"markDate"

@interface TeacherChildCheckOnCalendar()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *previous;
@property (weak, nonatomic) IBOutlet UIButton *next;
@property (weak, nonatomic) IBOutlet WQCalendarGridView *calContent;

@property (strong, nonatomic)NSArray *checkOnData;

@property (weak, nonatomic) IBOutlet UILabel *dayCountField;
@end

@implementation TeacherChildCheckOnCalendar{
    NSDateFormatter *dateFormatter1;
    NSDateFormatter *dateFormatter2;
    
    NSDate *displayDate;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    dateFormatter1 = [[NSDateFormatter alloc]init];
    [dateFormatter1 setDateFormat:@"yyyy年MM月"];
    
    dateFormatter2 = [[NSDateFormatter alloc]init];
    [dateFormatter2 setDateFormat:@"yyyy-MM-dd"];
    
    displayDate = [NSDate date];
    
    [self setTitleLableAndButtonLable];
    
    
    
    [self loadRemoteData:[NSDate date] init:YES];
    
    self.calContent.displayDate = [NSDate date];
    
    self.calContent.isForTeacher = YES;
}


-(void)loadRemoteData:(NSDate *)date init:(BOOL)init{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
//    NSString *uriString = [NSString stringWithFormat:@"http://115.28.20.171:8080/develop/iBaby365API/V3/Child/%@/markList.json", self.childDict[@"id"]];
    NSString *uriString = [NSString stringWithFormat:@"http://115.28.20.171/iBaby365API/V3/Child/%@/markList.json", self.childDict[@"id"]];
    NSDictionary *parameters = @{@"key": delegate.accessKey, @"startMarkDate": [dateFormatter2 stringFromDate:[date firstDayOfCurrentMonth]], @"endMarkDate":[dateFormatter2 stringFromDate:[date lastDayOfCurrentMonth]]};
    
    [[AFHTTPRequestOperationManager manager]GET:uriString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
        
        int resultCode = [responseObject[@"result"]intValue];
        if (resultCode == 0) {
            _checkOnData = responseObject[@"data"];
            self.calContent.checkOnData = self.checkOnData;
            [self.calContent reloadData];
            
            [self.dayCountField setText:[NSString stringWithFormat:@"本月未到总天数：%d天", [self.checkOnData count]]];
        }else{
            [UIAlertView alertWithRemoteError:resultCode];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"opernation: %@\nerror: %@", operation, error);
        [UIAlertView alertWithNetworkError];
    }];
}


- (IBAction)previousMonthAction:(id)sender {
    displayDate = [displayDate dayInThePreviousMonth];
    self.calContent.displayDate = displayDate;
    [self loadRemoteData:displayDate init:NO];
    
    [self setTitleLableAndButtonLable];
}
- (IBAction)nextMonthAction:(id)sender {
    displayDate = [displayDate dayInTheFollowingMonth];
    self.calContent.displayDate = displayDate;
    [self loadRemoteData:displayDate init:NO];
    
    [self setTitleLableAndButtonLable];
}

/**
 *  更新日期label和左右的button上的月份
 */
-(void)setTitleLableAndButtonLable{
    [self.titleLabel setText:[dateFormatter1 stringFromDate:displayDate]];
    [self.previous setTitle:[[displayDate dayInThePreviousMonth]monthStringWithChinese]forState:UIControlStateNormal];
    [self.next setTitle:[[displayDate dayInTheFollowingMonth]monthStringWithChinese]forState:UIControlStateNormal];
}
@end
